home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / i / internet / software / inetcust / inetcust.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-04  |  6.7 KB  |  317 lines

  1. #include <tos.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include "inetcust.h"
  7. #include "cookie.h"
  8. #define _cookiejar    *(long *)0x5A0L
  9.  
  10. long set_cookiejar(COOKIE *);
  11.  
  12. extern char Start;
  13. extern INETCUST inetcust;
  14. extern COOKIE   cookiejar[];
  15. #define MAXCOOKIES 11
  16.  
  17. char buffer[200];
  18. INETCUST   cust;        /* temporary custom struct */
  19.  
  20. int atoet(char *a,char *et)
  21. {
  22.   int etbuf[6];
  23.   if(sscanf(a,"%2x:%2x:%2x:%2x:%2x:%2x",&etbuf[0],&etbuf[1],&etbuf[2],&etbuf[3],&etbuf[4],&etbuf[5]) == 6)
  24.   {
  25.     et[0] = (char)etbuf[0];
  26.     et[1] = (char)etbuf[1];
  27.     et[2] = (char)etbuf[2];
  28.     et[3] = (char)etbuf[3];
  29.     et[4] = (char)etbuf[4];
  30.     et[5] = (char)etbuf[5];
  31.     return(6);
  32.   }    
  33.   return(0);
  34. }
  35.  
  36. long atoin(char *a)
  37. {
  38. unsigned  n1,n2,n3,n4;
  39.  
  40.   if(sscanf(a,"%u.%u.%u.%u",&n1,&n2,&n3,&n4) == 4)
  41.     return(((unsigned long)n1<<24) + ((unsigned long)n2<<16) + ((unsigned long)n3<<8) +(unsigned long)n4);
  42.   return(0L);
  43. }
  44.  
  45.  
  46. int loadinf(INETCUST *cust)
  47. {
  48. FILE *p_inf;
  49. FILE *p_hosts;
  50. char *p_tok;
  51. char *p_arg;
  52. int ntimeserv = 0;
  53. int nnameserv = 0;
  54.  
  55.     p_inf=fopen("\\inetcust.inf","r");
  56.     if(!p_inf) return(0);
  57.     cust->magic = sizeof(INETCUST);
  58.     while(fgets(buffer,(int)sizeof(buffer)-1,p_inf) != NULL)
  59.     {
  60.         p_tok = strtok(buffer,"= \n");
  61.         if(!p_tok || !strlen(p_tok)) continue;
  62.         p_arg = strtok(NULL," \n");
  63.         
  64.         if(!stricmp(p_tok,"HADDR"))
  65.         {
  66.             if(!atoet(p_arg,cust->haddr))
  67.             {
  68.                 printf("invalid hardware address 'p_arg'\n");
  69.                 fclose(p_inf);
  70.                 return(0);
  71.             }
  72.         }
  73.         else if(!stricmp(p_tok,"HOSTIP"))
  74.         {
  75.             cust->inaddr = atoin(p_arg);
  76.             if(!cust->inaddr)
  77.             {
  78.                 printf("invalid internet address 'p_arg'\n");
  79.                 fclose(p_inf);
  80.                 return(0);
  81.             }
  82.         }
  83.         else if(!stricmp(p_tok,"SUBNETBITS"))
  84.         {
  85.             cust->subnetbits = atoi(p_arg);
  86.         }
  87.         else if(!stricmp(p_tok,"TCPWINDOW"))
  88.         {
  89.             cust->tcpwnd = atoi(p_arg);
  90.             if(!cust->tcplowater)
  91.                 cust->tcplowater = cust->tcpwnd/2;
  92.         }
  93.         else if(!stricmp(p_tok,"TCPLOWATER"))
  94.         {
  95.             cust->tcplowater = atoi(p_arg);
  96.         }
  97.         else if(!stricmp(p_tok,"NETMEM"))
  98.         {
  99.             cust->netmem = atol(p_arg);
  100.         }
  101.         else if(!stricmp(p_tok,"USER"))
  102.         {
  103.             strncpy(cust->username,p_arg,MAXUSERNAME-1);
  104.         }
  105.         else if(!stricmp(p_tok,"HOSTS"))
  106.         {
  107.             if((p_hosts = fopen(p_arg,"r")) == NULL)
  108.             {
  109.                 printf("cannot find '%s'\n",p_arg);
  110.                 fclose(p_inf);
  111.                 return(0);
  112.             }
  113.             fclose(p_hosts);
  114.             strncpy(cust->hosts,p_arg,sizeof(cust->hosts)-1);
  115.         }
  116.         else if(!stricmp(p_tok,"PASSWD"))
  117.         {
  118.             strncpy(cust->passwd,p_arg,sizeof(cust->passwd)-1);
  119.         }
  120.         else if(!stricmp(p_tok,"TZOFFS"))
  121.         {
  122.             cust->gmtoffs = atoi(p_arg);
  123.         }
  124.         else if(!stricmp(p_tok,"TZ"))
  125.         {
  126.             strncpy(cust->tzname,p_arg,sizeof(cust->tzname-1));
  127.         }
  128.         else if(!stricmp(p_tok,"TIMESERVER"))
  129.         {
  130.             if(ntimeserv > 1)
  131.             {
  132.                 printf("no room for additional timeserver\n");
  133.                 fclose(p_inf);
  134.                 return(0);
  135.             }
  136.             if(!ntimeserv) cust->timeserver[1] = 0L;
  137.             cust->timeserver[ntimeserv] = atoin(p_arg);
  138.             ntimeserv++;
  139.         }
  140.         else if(!stricmp(p_tok,"NAMESERVER"))
  141.         {
  142.             if(nnameserv > 2)
  143.             {
  144.                 printf("no room for additional nameserver\n");
  145.                 fclose(p_inf);
  146.                 return(0);
  147.             }
  148.             if(!nnameserv) 
  149.             {
  150.                 cust->nameserver[1] = 0L;
  151.                 cust->nameserver[2] = 0L;
  152.             }
  153.             cust->nameserver[nnameserv] = atoin(p_arg);
  154.             nnameserv++;
  155.         }
  156.         else if(!stricmp(p_tok,"GATEWAY"))
  157.         {
  158.             cust->gateway = atoin(p_arg);
  159.         }
  160.         else if(!stricmp(p_tok,"HOSTNAME"))
  161.         {
  162.             strncpy(cust->hostname,p_arg,MAXHOSTNAME-1);
  163.         }
  164.         else if(!stricmp(p_tok,"DOMAIN"))
  165.         {
  166.             strncpy(cust->domainname,p_arg,MAXNAME-1);
  167.         }
  168.         else if(!stricmp(p_tok,"MAILHOST"))
  169.         {
  170.             strncpy(cust->mailhost,p_arg,MAXNAME-1);
  171.         }
  172.         else if(!stricmp(p_tok,"PRINTHOST"))
  173.         {
  174.             strncpy(cust->printhost,p_arg,MAXNAME-1);
  175.         }
  176.         else if(!stricmp(p_tok,"LOGINHOST"))
  177.         {
  178.             strncpy(cust->loginhost,p_arg,MAXNAME-1);
  179.         }
  180.         else if(!stricmp(p_tok,"PRINTER"))
  181.         {
  182.             strncpy(cust->printdev,p_arg,MAXHOSTNAME-1);
  183.         }
  184.         else if(!stricmp(p_tok,"FTPWIN"))
  185.         {
  186.             cust->ftpwin = atol(p_arg);
  187.         }
  188.         else
  189.         {
  190.             printf(" unknown keyword '%s'\n",p_tok);
  191.         }
  192.     }
  193.     fclose(p_inf);
  194.     return(1);
  195. }
  196.  
  197.  
  198. COOKIE *get_cookiejar(void)
  199. {
  200.   register long superstack;
  201.   register COOKIE *jar;
  202.  
  203.   superstack = Super((void *)0);
  204.   jar = (COOKIE *)_cookiejar;  /* return address of cookiejar */
  205.   Super((void*)superstack);
  206.   return(jar);
  207. }
  208.  
  209.  
  210. COOKIE *add_cookie(long id,long val)
  211. {
  212.   register COOKIE *jar;
  213.   jar = get_cookiejar();
  214.   while(jar->id != ENDCOOKIE) jar++;  /* search end of cookiejar */
  215.   jar->id = id;
  216.   (jar+1)->val = jar->val;  /* keep size of jar */
  217.   jar->val = val;
  218.   (jar+1)->id = ENDCOOKIE;
  219.   return(jar);
  220. }
  221.  
  222.  
  223. COOKIE *get_cookie(long id)
  224. {
  225.   register COOKIE *jar;
  226.   if((jar = get_cookiejar()) == NULL) return(NULL);
  227.   while(jar->id)
  228.   {
  229.     if(jar->id == id) return(jar);  /* find cookie */
  230.     jar++;
  231.   }
  232.   return(NULL);
  233. }
  234.  
  235.  
  236. long set_cookiejar(COOKIE *newjar)
  237. {
  238. long superstack;
  239. register COOKIE *jar;
  240. register long i,c;
  241.  
  242.   jar = get_cookiejar();
  243.   i=0;
  244.   if(jar) for(; jar[i].id != ENDCOOKIE; i++);  /* get size of old jar */
  245.   if(jar && jar[i].val > i) return(-jar[i].val); /* we have room */
  246.   if(jar && jar[i].val >= MAXCOOKIES) return(0); /* occupied jar bigger than my pool */
  247.   
  248.   if(jar && newjar) for(c=0; c<i; c++)
  249.   {  /* copy old jar */
  250.     newjar[c].id = jar[c].id;
  251.     newjar[c].val = jar[c].val;
  252.   }
  253.   newjar[i].id = ENDCOOKIE;  /* terminate new jar */
  254.   newjar[i].val = MAXCOOKIES;
  255.  
  256.   superstack = Super((void *)0);
  257.   _cookiejar = (long)newjar;  /* install new jar */
  258.   Super((void*)superstack);
  259.   return(MAXCOOKIES);
  260. }
  261.  
  262.  
  263.  
  264.  
  265.  
  266. long main()
  267. {
  268. COOKIE *jar;
  269. COOKIE *cookie;
  270. long len = (char *)cookiejar - &Start + sizeof(BASPAG);
  271. long jlen;
  272. char str[10];
  273.  
  274.     jar = get_cookiejar();
  275.     if(jar)
  276.     {
  277.         cookie = get_cookie(INETCUSTCOOKIE);
  278.         if(cookie) 
  279.         {
  280.             memcpy(&cust,(void *)(cookie->val),sizeof(INETCUST));
  281.             if(!loadinf(&cust))
  282.                 Cconws("INETCUST: cannot load INETCUST.INF!\a\r\n");
  283.             else
  284.             {
  285.                 memcpy((void *)(cookie->val),&cust,sizeof(INETCUST));
  286.                 Cconws("INETCUST updated !\r\n");
  287.             }
  288.             Pterm(1);
  289.         }
  290.     }
  291.     jlen = set_cookiejar(cookiejar);
  292.     if(!jlen) 
  293.     {
  294.         Cconws("INETCUST: cannot add cookie!\r\n");
  295.         Pterm(1);
  296.     }
  297.     if(jlen > 0) 
  298.     {
  299.         len += MAXCOOKIES*sizeof(COOKIE);
  300.         Cconws("created new cookie jar\r\n");
  301.     }
  302.     memset(&inetcust,0,sizeof(INETCUST));
  303.     if(!loadinf(&inetcust))
  304.     {
  305.         Cconws("INETCUST: cannot load INETCUST.INF!\a\r\n");
  306.         Pterm(1);
  307.     }
  308.     
  309.     add_cookie(INETCUSTCOOKIE,(long)&inetcust);
  310.     
  311.     Cconws("INETCUST network environment installed\r\nusing ");
  312.     Cconws(ltoa(len,str,10));
  313.     Cconws(" bytes\r\n");
  314.     return(len);
  315. }
  316.  
  317.